package com.bili.bili_data_api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bili.bili_data_api.dao.mapper.UserMapper;
import com.bili.bili_data_api.dao.pojo.User;
import com.bili.bili_data_api.service.UserService;
import com.bili.bili_data_api.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    UserMapper userMapper;
    //登录
    @PostMapping(value = {"/login"},produces = {"application/json; charset=UTF-8"})
    public Result login(@RequestBody User param){
        User temp;
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getAccount,param.getAccount());
        temp=userMapper.selectOne(userLambdaQueryWrapper);
        if(temp==null){
            return Result.error().code(2001);//2001表示没有该用户
        }
        else if(!temp.getPwd().equals(param.getPwd())){
            return Result.error().code(2002);//2002表示密码不正确
        }
//        System.out.println(temp.getIntroduction());
        return Result.ok().data("user",temp);
    }


    //注册
    @PostMapping(value = {"/register"},produces = {"application/json; charset=UTF-8"})
    public Result register(@RequestBody User user){
        User temp;
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getAccount,user.getAccount());
        temp=userMapper.selectOne(userLambdaQueryWrapper);
        if(temp!=null){
            return Result.error().code(2001);//2001表示该用户已存在
        }
        userMapper.insert(user);
        return Result.ok();
    }

    @PostMapping(value = {"/edit"},produces = {"application/json; charset=UTF-8"})
    public Result edit(@RequestBody User user){
//        System.out.println(user.getAccount());
//        System.out.println(user.getHobby());
        return userService.edit(user);
    }
}
